import org.omg.CORBA.TIMEOUT;

import java.time.Instant;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.*;

/**
 * @author wangxiyue@cyou-inc.com
 * @date 2019-08-29
 * @since 1.0.0
 */
public class ScheduledExecutorServiceTest {

    public static void main(String[] args) {
//        demo1();
//        demo2();
//        demo3();
        demo4();

    }


    public static void demo1() {

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        ScheduledFuture<?> schedule =
                scheduledExecutorService.schedule(() -> System.out.println(Instant.now().getEpochSecond() + "执行了"), 5,
                        TimeUnit.SECONDS);
//        boolean cancel = schedule.cancel(true);
    }

    private static void demo2() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        ScheduledFuture<String> schedule =
                scheduledExecutorService.schedule(() ->
                                LocalTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "执行完毕"
                        , 2,
                        TimeUnit.SECONDS);
        // 会在此处进行阻塞
        try {
            System.out.println(schedule.get());
            scheduledExecutorService.shutdown();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static void demo3() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println(LocalTime.now().format(DateTimeFormatter.ISO_TIME) + " 定时任务执行了.");
        }, 1, 2, TimeUnit.SECONDS);
    }

    private static void demo4() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(
                () -> System.out.println(LocalTime.now().format(DateTimeFormatter.ISO_TIME) + " 定时任务执行了."),
                1, 2, TimeUnit.SECONDS);
    }


}
